import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import AddEditModal from '#cpt/temp/AddEditModal'
import Delete from '#cpt/temp/Delete'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const ChangeLeaderModal = $Lazy.state(()=>import('./cpt/change-leader-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '区域名称',		name: 'region_name' },
			{ label: '区域编号',		name: 'region_num' },
			{ label: '状态',		name: 'status', 	type:'select',data:[{name:'启用',value:'1'},{name:'未启用',value:'0'}] },
		],
		// 提交表单列表
		formData : [
			{ label: '区域名称',		name: 'region_name', 		half:true, 	required:true },
			{ label: '区域编号',		name: 'region_num', 		half:true, 	required:true },
			{ label: '上级区域',		name: 'pid', 		half:true, 	type:'select' },
			{ label: '负责人',		name: 'leader_id', 			half:true, 	required:true, 	type:'select', nameStr:'name', },
			{ label: '排序',		name: 'sort', 				half:true, 	type:'number', value:0  },
			{ label: '状态',		name: 'status', 			half:true, 	required:true,type:'select', nameStr:'name',data:[{name:'启用',value:'1'},{name:'未启用',value:'0'}] },
		]
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
			this.fetch()
		}
	}
	//获取下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'leader_id', 'pid' ], ()=>{
			$cache.then( async f => {
				// 负责人
				$fn.setRow(formData, 'leader_id', await f.getCache({ api:'salesman' } ) )
				// 区域
				$fn.setRow(formData, 'pid', await f.getCache({ api:'bs_area' } ) )
				
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bs-area/index')
	
	// 表单
	cols = [
		{ title: '区域名称',		dataIndex: 'region_name', 	width:150 },
		{ title: '区域编号', 	dataIndex: 'region_num', 	...$fn.table.num},
		{ title: '上级区域', 	dataIndex: 'parent_path', 	width:150 },
		{ title: '负责人', 		dataIndex: 'real_name', 	...$fn.table.name },
		{ title: '层级',		dataIndex: 'level', 		...$fn.table.num },
		{ title: '状态', 		dataIndex: 'status', 		width:150,   render: text => +text === 1 ? '启用' : '未启用'},
		TableControls.controls.call(this, { width: 200 })
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => {
		const { id } = rows
		return [
			TableControls.edit.call(this, 'bs-area/info', { id }, {
				idStr:'id',
				nameStr:'id',
				select: this.getModalSelect,
				click: ()=> $fn.disabledRows(this.state.formData, ['leader_id'], true),
			}),
			this.onDel([id]),
			Controls.click.call(this, '更换负责人', ()=> this.changeLeaderModalRef.open( rows ))
		]
	}
	
	// 操作按钮
	controls = () => [
		Controls.add.call(this, { click: ()=>{
			const { formData } = this.state
			this.getModalSelect()
			$fn.disabledRows(formData, ['leader_id'], false)
			this.setState({ formData })
		} }),
		this.onDel(),
	]
	// 删除
	onDel = uuid => Delete.call(this, 'bs-area/del', { uuid, idStr:'id', isKeys: !uuid, onComplete:()=> $fn.dataRemove('bs-area/select') })
	
	render(){
		return (
			<>
				<Page title='业务区域' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { idStr:'id', isInit: window.isInit }) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s6', width:'40%', minWidth:650, api:'bs-area/add', editApi: 'bs-area/edit', onComplete:()=> $fn.dataRemove('bs-area/select') }) }
				{/* 更换负责人 */}
				<ChangeLeaderModal onRef={ref=>this.changeLeaderModalRef = ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index